import os
import psutil
import time
import logging
import subprocess
from threading import Thread
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s line:%(lineno)d %(levelname)s:%(message)s',
                    datefmt='%H:%M:%S',
                    )

class PsPs():
    def __init__(self, pid=None):
        self.log = "\n"

        self.p = psutil.Process(pid)

        
    def psps(self, p, depth):
        memory = 0
        try:
            memory = "%.1fMB-" % (p.memory_info().rss / 1024 / 1024)
            cmd_line = " ".join(p.cmdline())
        except:
            cmd_line = p.name()
        self.log += "  |" * depth + memory + cmd_line + "\n"
        for pp in p.children():
            if pp.pid == p.pid:
                continue
            self.psps(pp, depth + 1)

    def run(self):
        self.log = "\n"
        self.psps(self.p, 0)
        logging.info(self.log)
    
    def loop(self):
        while True:
            self.run()
            time.sleep(3)

    def main(self):
        t = Thread(target=self.loop, daemon=True)
        t.start()


def run():
    subprocess.run("python b.py")

t = Thread(target=run)
t.start()

PsPs(4).main()
time.sleep(55555)





